package com.example.taobao

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.SpannableStringBuilder
import kotlinx.android.synthetic.main.activity_myxinxi.*
import kotlinx.android.synthetic.main.newaddress.*
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.lang.Exception
import java.net.URLEncoder
import kotlin.concurrent.thread

class myxinxi : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_myxinxi)
        val prefs = getSharedPreferences("data",Context.MODE_PRIVATE)
        val name = prefs.getString("name","未登录")
        val zhanghao = prefs.getString("zhangh","无")
        val editable: Editable = SpannableStringBuilder(name)
        val editable1: Editable = SpannableStringBuilder(zhanghao)
        nichen1.text = editable
        zhanghao1.text = editable1
        exitlogin.setOnClickListener {
            val editor = getSharedPreferences("data", Context.MODE_PRIVATE)?.edit()
            editor?.clear()
            editor?.commit()
            intent.setClass(this,zhuyem::class.java)
            startActivity(intent)
        }
        alterxin.setOnClickListener {
            thread {
                try {
                    val client = OkHttpClient()
                    val requestBody = FormBody.Builder()
                        .add("name", URLEncoder.encode(nichen1.text.toString(), "utf-8"))
                        .add("userzhanghao", URLEncoder.encode(zhanghao1.text.toString(), "utf-8"))
                        .add("oldzhanghao", URLEncoder.encode(zhanghao, "utf-8"))
                        .build()
                    val request = Request.Builder()
                        .url("http://192.168.43.140:8080/aaa_war_exploded/alteruser.jsp")
                        .post(requestBody)
                        .build()
                    val respone = client.newCall(request).execute()
                    val responeData = respone.body?.string()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                val editor=getSharedPreferences("data", Context.MODE_PRIVATE)?.edit()
                editor?.putString("name",nichen1.text.toString())
                editor?.putString("zhangh",zhanghao1.text.toString())
                val intent: Intent = Intent()
                intent.setClass(this, zhuyem::class.java)
                startActivity(intent)
            }
        }
    }
}